﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;

namespace Zwms.Client.ManageWms
{
    public partial class StockAdjustWnd : UserControl
    {
        #region 成员变量
        private StoGoodsStock _selectedRecord;
        private PagerIndex _pagerIndex;
        private StoGoodsStock _queryRecord;
        #endregion

        #region 构造函数
        public StockAdjustWnd()
        {
            InitializeComponent();
            InitBtnImage();
            _selectedRecord = new StoGoodsStock();
            _pagerIndex = new PagerIndex(1, CommonDefine.iPageSize);
            this.tbsWarehouse.Tag = MainForm.CurrentUser.WarehouseCode;
            this.tbsWarehouse.SetTextBoxContent(MainForm.CurrentUser.WarehouseName);
            if (MainForm.CurrentUser.UserRole.Trim() == "03")
            {
                this.tbsWarehouse.SetEditState(false);
            }
            else
            {
                this.tbsWarehouse.SetEditState(true);
            }
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnQuery.Image = global::Zwms.Client.Properties.Resources.view;
            this.btnEdit.Image = global::Zwms.Client.Properties.Resources.edit;
            this.btnOpenBox.Image = global::Zwms.Client.Properties.Resources.openbox;
            this.btnPkgBox.Image = global::Zwms.Client.Properties.Resources.box;
            this.btnTranfer.Image = global::Zwms.Client.Properties.Resources.refresh;
        }

        private void ResetDataPager()
        {
            dataPager.PageCurrent = 1;
            dataPager.PageSize = CommonDefine.iPageSize;
        }

        private void GetRecordFromUI()
        {
            this._queryRecord = new StoGoodsStock();
            this._queryRecord.Id = this.tbStockCode.Text.Trim();
            this._queryRecord.GoodsCode = this.tbGoodsCode.Text.Trim();
            this._queryRecord.WarehouseCode = this.tbsWarehouse.Tag.ToString();
            this._queryRecord.AreaCode = this.tbsArea.Tag.ToString();
            this._queryRecord.ShelfCode = this.tbsShelf.Tag.ToString();
            this._queryRecord.LocationCode = this.tbsLocation.Tag.ToString();  
        }

        private void LoadStockDetail(StoGoodsStock record)
        {
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            try
            {
                this.Cursor = Cursors.WaitCursor;
                IList<StoGoodsStock> resultList = client.QueryGoodsStockList(record, ref _pagerIndex);
                this.stoGoodsStockBindingSource.DataSource = null;
                this.stoGoodsStockBindingSource.DataSource = resultList;
                this.dataPager.RecordCount = _pagerIndex.RecordCount;
                this.Cursor = Cursors.Default;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        #endregion

        #region 控件函数
        private void tbsWarehouse_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.WarehouseWnd uc = new InfoWms.WarehouseWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择仓库");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsWarehouse.SetTextBoxContent(uc.SelectedRecord.WarehouseName);
                tbsWarehouse.Tag = uc.SelectedRecord.Id;
            }
        }

        private void tbsArea_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.AreaWnd uc = new InfoWms.AreaWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择库区");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsArea.SetTextBoxContent(uc.SelectedRecord.AreaName);
                tbsArea.Tag = uc.SelectedRecord.AreaCode;
            }
        }

        private void tbsShelf_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.ShelfWnd uc = new InfoWms.ShelfWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择货架");
            uc.SetQueryState();
            if (window.ShowDialog() == DialogResult.OK)
            {
                tbsShelf.SetTextBoxContent(uc.SelectedRecord.ShelfName);
                tbsShelf.Tag = uc.SelectedRecord.ShelfCode;
            }
        }

        private void tbsLocation_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.LocationWnd uc = new InfoWms.LocationWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择库位");
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsLocation.SetTextBoxContent(uc.SelectedRecord.LocationName);
                tbsLocation.Tag = uc.SelectedRecord.LocationCode;
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            this.GetRecordFromUI();
            _pagerIndex.CurrentPageIndex = 1;
            this.LoadStockDetail(this._queryRecord);
        }

        private void dataPager_BtnClick(object sender, EventArgs e)
        {
            _pagerIndex.CurrentPageIndex = dataPager.PageCurrent;
            this.LoadStockDetail(this._queryRecord);
        }


        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgv.CurrentCell == null||dgv.CurrentCell.RowIndex<0)
            {
                MessageBox.Show("请选择要调整的库存记录！");
                return;
            }
            int rowIndex = this.dgv.CurrentCell.RowIndex;
            this._selectedRecord = dgv.Rows[rowIndex].DataBoundItem as StoGoodsStock;
            StockAdjustForm window = new StockAdjustForm(this._selectedRecord);
            if (window.ShowDialog() == DialogResult.OK)
            {
                InfoSys.SysLogWriter.WriteOptLog("调整库存", "StockAdjust", "更新");
                this.LoadStockDetail(this._queryRecord);
            }
        }

        private void btnOpenBox_Click(object sender, EventArgs e)
        {
            if (dgv.CurrentCell == null || dgv.CurrentCell.RowIndex < 0)
            {
                MessageBox.Show("请选择要调整的库存记录！");
                return;
            }
            int rowIndex = this.dgv.CurrentCell.RowIndex;
            this._selectedRecord = dgv.Rows[rowIndex].DataBoundItem as StoGoodsStock;
            StockOpenBoxForm window = new StockOpenBoxForm(this._selectedRecord);
            if (window.ShowDialog() == DialogResult.OK)
            {
                InfoSys.SysLogWriter.WriteOptLog("整箱开箱", "OpenBox", "开箱");
                this.LoadStockDetail(this._queryRecord);
            }
        }

        private void btnPkgBox_Click(object sender, EventArgs e)
        {
            if (dgv.CurrentCell == null || dgv.CurrentCell.RowIndex < 0)
            {
                MessageBox.Show("请选择要调整的库存记录！");
                return;
            }
            int rowIndex = this.dgv.CurrentCell.RowIndex;
            this._selectedRecord = dgv.Rows[rowIndex].DataBoundItem as StoGoodsStock;
            StockPkgBoxForm window = new StockPkgBoxForm(this._selectedRecord);
            if (window.ShowDialog() == DialogResult.OK)
            {
                InfoSys.SysLogWriter.WriteOptLog("散件合箱", "PkgBox", "合箱");
                this.LoadStockDetail(this._queryRecord);
            }

        }

        private void btnTranfer_Click(object sender, EventArgs e)
        {
            if (dgv.CurrentCell == null || dgv.CurrentCell.RowIndex < 0)
            {
                MessageBox.Show("请选择要调整的库存记录！");
                return;
            }
            int rowIndex = this.dgv.CurrentCell.RowIndex;
            this._selectedRecord = dgv.Rows[rowIndex].DataBoundItem as StoGoodsStock;
            TransferGoodsForm window = new TransferGoodsForm(this._selectedRecord);
            if (window.ShowDialog() == DialogResult.OK)
            {
                InfoSys.SysLogWriter.WriteOptLog("货物移库", "GoodsTsf", "移库");
                this.LoadStockDetail(this._queryRecord);
            }
        }
        #endregion


    }
}
